package com.huang.wangpan.controller;

import com.huang.wangpan.dao.UserDao;
import com.huang.wangpan.vo.User;
import com.huang.wangpan.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class IndexController {
    @Autowired
    UserDao userDao;

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @GetMapping("/")
    public String index(HttpServletRequest request, Model model) {
        String token = CommonUtils.loadToken(request);
        if (StringUtils.isEmpty(token)) {
            return "index";
        }

        logger.info("老子终于有token了：token={}", token);
        User user = userDao.searchByToken(token);
        if (user == null) { // 处理空指针异常
            logger.info("虽然有token,但user为空；跳转登录页");
            return "login";
        }
        String name = user.getName();
        // request.getSession().setAttribute("user",user);
        model.addAttribute("name", name);
        // model.addAttribute("img","/static/timg.jpg");
        // userData.setId(user.getAccountid());
        return "interface";
        // return "redirect:/interface";

    }

    @GetMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

    @PostMapping("/register")
    public String register(User user, HttpServletResponse response) {
        if (user == null || StringUtils.isEmpty(user.getName()) || StringUtils.isEmpty(user.getPassword())) {
            logger.error("注册输入参数不合法，请重新输入");
            return "register";
        }
        // 先去数据库查询是否存在：
        User exsit = userDao.selectByName(user.getName());
        if (exsit != null) {
            logger.error("你的用户名已经存在，请重新注册");
            return "register";
        }
        String token = UUID.randomUUID().toString();
        user.setToken(token);
        user.setGmtcreate(System.currentTimeMillis() + "");
        user.setGmtmodified(System.currentTimeMillis() + "");
        userDao.insertByRegister(user);
        Cookie cookie = new Cookie("token", token);
        cookie.setMaxAge(1800);
        response.addCookie(cookie);
        return "login";
    }

    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @PostMapping("/login")
    public String login(User user, HttpServletResponse response, Model model) {
        if (user == null || StringUtils.isEmpty(user.getName()) || StringUtils.isEmpty(user.getPassword())) {
            logger.error("登录输入参数不合法，请重新登录！");
            return "login";
        }
        // 先去数据库查询是否存在：
        User exsit = userDao.selectByNameAndPwd(user.getName(), user.getPassword());
        if (exsit == null) {
            logger.error("你还未注册，请先去注册！");
            return "register";
        }
        Cookie cookie = new Cookie("token", exsit.getToken());
        cookie.setMaxAge(1800);
        response.addCookie(cookie);
        model.addAttribute("name", exsit.getName());
        return "interface";
    }

    @GetMapping("/toIndex")
    public String toIndex() {
        return "index";
    }
}
